//
//  ZCFGV2ViewModel.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/11/4.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "ZCFGV2ViewModel.h"

#import "ZCFGV2TableViewModel.h"

@implementation ZCFGV2ViewModel

- (void)initialize
{
    [super initialize];
    
    @weakify(self);
    
    _tabIndex = 0;
    
    NSMutableArray* newTableVMS = [NSMutableArray array];
    for (NSInteger index=0; index<2; index++)
    {
        ZCFGV2TableViewModel* tvm = [[ZCFGV2TableViewModel alloc] initWithParams:@{}];
        tvm.perPage = List_PageSize;
        tvm.lastPage = 1000;
        tvm.cellHeight = 112;
        /// 允许下拉刷新
        tvm.shouldPullDownToRefresh = YES;
        /// 允许上拉加载
        tvm.shouldPullUpToLoadMore = YES;
        tvm.tag = 10+index;
        
        newTableVMS[newTableVMS.count] = tvm;
    }
    self.tableVMs = newTableVMS;
    
    [[RACObserve(self, tabIndex) distinctUntilChanged] subscribeNext:^(id x) {
        @strongify(self)
        
        //判断当前是否需要请求数据
        NSInteger curTabIndex = [x integerValue];
        
        ZCFGV2TableViewModel* curTVM = self.tableVMs[curTabIndex];
        
        if (curTVM.dataSource.count == 0)
        {
            [curTVM.requestDataCmd execute:@{}];
        }
    }];
}

@end
